cap log close
log using "$results/3a_sic_analysis_tables.log", replace

***************************
**# Table 1: Balance tests
***************************
qui {
**# Setup
use "analysis/SIC87_regression_Dataset.dta", clear
tostring sic, gen(sic4)
merge m:1 sic4 using "analysis/com_controls_clean"
drop if _m == 2
drop _m
keep if year==1979

global tariff_controls  dln_pstar_vw_7279_alt mfa rho other_tariff_change
global prod_controls ln_cap_lab_78 skill_emp_share_78 women_share mat_ship_def_78
global rti_controls ln_invest_def_78 lag_invest_def_ch rti automation78 
global controls_1930 hp_per_worker_1930 skilled_share_1930 female_share_1930 mat_ship_1930 invest_1930

egen full = rowmiss($tariff_controls $prod_controls $rti_controls ave_ols_ch9 ave_iv_ch9  $controls_1930 )
keep if full==0
drop full 

**# Regressions
eststo clear
eststo: reg  ave_iv_ch9 $tariff_controls, robust 
eststo: reg  ave_iv_ch9 $prod_controls, robust 
eststo: reg  ave_iv_ch9 $rti_controls, robust 
eststo: reg  ave_iv_ch9 $tariff_controls $prod_controls $rti_controls, robust
eststo: reg  ave_iv_ch9 $tariff_controls $prod_controls $rti_controls $controls_1930, robust

**# Make table
noi di ""
noi di "**************************************************"
noi di "****************** Table 1 ***********************"
noi di "**************************************************"
noi esttab, b(%9.3f) se(%9.3f) r2(%9.3f) obslast star(* 0.10 ** 0.05 *** 0.01)

esttab  using "$results/table_1.tex", ///
se(%9.3f) ///
b(%9.3f) ///
star(* .1  ** .05 *** .01) ///
nomtitles /// 
mgroups(" \$ \Delta \ln(1+AVE_{i}^{IV} )\$ ", ///
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span})) ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
r2 ///
replace	
}

**********************************
**# Tables 2 & A3: Trade outcomes 
********************************** 
qui {
**# Setup
use "analysis/SIC87_regression_Dataset.dta", clear
keep if year==1979

egen full = rowmiss(ave_ols_ch9 ave_iv_ch9 ln_imp imp_pen ln_exp exp_ship   $tariff_controls )   		
keep if full==0
		
**# Regressions
		
forval t = 1/2{
	
	eststo clear
	if `t'==1 loc pre
	if `t'==1 loc n 2
	if `t'==2 loc pre lag_
	if `t'==2 loc n "A3"

	sum ave_ols_ch9,d
	loc iqrx `=`r(p75)'-`r(p25)''

	sum ln_imp_ch, d
	loc iqry `=`r(p75)'-`r(p25)''

	eststo m11: reg `pre'ln_imp_ch ave_ols_ch9 if year == 1979, robust
	estadd loc spec = "OLS"
	eststo m11, add(UnderID_p  e(idp))
	eststo m11, add(Weak_IV_KP e(widstat))

	loc beta = _b[ave_ols_ch9]

	loc iqe = `=(`beta'*`iqrx')/(`iqry')'
	estadd  scalar IQR = `iqe'

	loc ctrl2 
	loc ctrl3  		   dln_pstar_vw_7279_alt rho 
	loc ctrl4  `ctrl3' mfa other_tariff_change 
	loc ctrl5  `ctrl4' 
	loc ctrl6  `ctrl4' 
	loc ctrl7  `ctrl4' 

	loc yvar2 ln_imp
	loc yvar3 ln_imp
	loc yvar4 ln_imp
	loc yvar5 imp_pen
	loc yvar6 ln_exp
	loc yvar7 exp_ship

	forval i = 2/7 {

		sum `pre'`yvar`i''_ch,d
		loc iqry `=`r(p75)'-`r(p25)''
		sum ave_ols_ch9,d
		loc iqrx `=`r(p75)'-`r(p25)''

		reg ave_ols_ch9  ave_iv_ch9  `ctrl`i'' if year == 1979, robust
		loc fsb = _b[ave_iv_ch9]
		eststo m1`i': ivreg2  `pre'`yvar`i''_ch (ave_ols_ch9  = ave_iv_ch9) `ctrl`i'' if year == 1979, robust
		estadd loc spec = "IV"
		eststo m1`i', add(UnderID_p  e(idp))
		eststo m1`i', add(Weak_IV_KP e(widstat))
		eststo m1`i', add(FSB        `fsb')
		loc beta = _b[ave_ols_ch9]
		loc iqe = `=(`beta'*`iqrx')'
		estadd  scalar IQR = `iqe'
	}
	
			
**# Make tables: baseline (79-87) & placebo (72-79)	
noi di ""
noi di "**************************************************"
noi di "****************** Table `n' ***********************"
noi di "**************************************************"

noi esttab m1*, b(%9.3f) se(%9.3f) r2(%9.3f) star(* 0.10 ** 0.05 *** 0.01) ///
    stats(IQR spec N FSB Weak_IV_KP, ///
    label("IQR" "Estimator" "Obs." "$1^{st}$ Stage Coeff." "KP F-Stat") ///
    fmt(%04.3fc %3s %9.0fc %04.3fc %04.3fc))

esttab m1* using "$results/table_`n'.tex", ///
se(%9.3f) ///
b(%9.3f) ///
star(* .1  ** .05 *** .01) ///
mtitles("$\Delta \ln(Imp_{i})\$" ///
"$\Delta \ln(Imp_{i})\$" ///
"$\Delta \ln(Imp_{i})\$" ///
"$\Delta \ln(Imp_{i})\$" ///
"$\Delta ImpPen_{i}\$" ///
"$\Delta \ln(Exp_{i})\$" /// 
"$\Delta \frac{Exp_i}{Ship_i}\$" 	///
) ///
stats(IQR spec N   FSB  Weak_IV_KP,  ///
label(	 "IQE" "Estimator"  /// 
"Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt( %04.3fc   %3s %9.0fc    %04.3fc   %04.3fc ) ///
) ///		
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace	
	}
}
	
********************************************
**# Tables 3 & A10: Baseline pay inequality
********************************************

qui {
**# Setup
use "analysis/SIC87_regression_Dataset.dta", clear
global tariff_controls  dln_pstar_vw_7279_alt rho  mfa   other_tariff_change
keep if year==1979

loc ctrl1 
loc ctrl2 `ctrl1' $tariff_controls
loc ctrl3 `ctrl2' $prod_controls
loc ctrl4 `ctrl3' $rti_controls 

loc ctrl1_lab "None"
loc ctrl2_lab "+Trade"
loc ctrl3_lab "+Prod."
loc ctrl4_lab "+Tech."

egen full = rowmiss(`ctrl4')
keep if full==0
drop full

loc lab1 	"\$ \Delta \ln(\frac{Pay_i^{Non-Prod}}{Pay_i^{Prod}}) \$"
loc lab2 	"\$ \Delta \ln(\frac{Emp_i^{Non-Prod}}{Emp_i^{Prod}}) \$"
loc lab3 	"\$ \Delta \ln(\frac{Wage_i^{Non-Prod}}{Wage_i^{Prod}}) \$"


**# Regressions
loc j = 0

eststo clear 

foreach outcome in ln_skill_pay_diff ln_skill_emp_diff ln_wsp {

	loc ++j
	loc title "`lab`j''"

	ivreg2 `outcome'_ch (ave_ols_up_ch9 = ave_iv_ch9  ) `ctrl4' if year==1979, robust
	gen fsamp = e(sample)

	sum ave_ols_ch9,d
	loc iqrx `=`r(p75)'-`r(p25)''

	sum `outcome'_ch, d
	loc iqry `=`r(p75)'-`r(p25)''

	forval i = 1/4{	
	reg   ave_ols_ch9   ave_iv_ch9  `ctrl`i'' , robust
	loc fsb = _b[ave_iv_ch9]

	eststo m`j'`i': ivreg2 `outcome'_ch  (ave_ols_ch9  = ave_iv_ch9  )  `ctrl`i''  , robust
	eststo m`j'`i', add(UnderID_p  e(idp))
	eststo m`j'`i', add(Weak_IV_KP e(widstat))
	eststo m`j'`i', add(FSB        `fsb')
	loc beta = _b[ave_ols_ch9]
	loc iqe = `=(`beta'*`iqrx')'
	estadd  scalar IQR = `iqe'
	estadd loc controls "`ctrl`i'_lab'"
	estadd loc spec "IV"
}	

drop fsamp
				
}

**# Make tables

**## Table 3: Baseline
noi di ""
noi di "**************************************************"
noi di "****************** Table 3 ***********************"
noi di "**************************************************"
noi esttab m1*, b(%9.3f) se(%9.3f) r2(%9.3f) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(IQR spec controls N   FSB  Weak_IV_KP,  ///
	label( "IQE" 	"Estimator" "Controls"  /// 
	"Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
	fmt( %04.3fc  %3s %10s %9.0fc  %04.3fc   %04.3fc ))
	
esttab  m1* using "$results/table_3.tex", ///
se(%9.3f) ///
b(%9.3f) ///
star(* .1  ** .05 *** .01) ///
stats(IQR spec controls N   FSB  Weak_IV_KP,  ///
label( "IQE" 	"Estimator" "Controls"  /// 
"Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt( %04.3fc  %3s %10s %9.0fc  %04.3fc   %04.3fc ) ///
) ///	
nomtitles ///
mgroups(" `title' ", ///
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span})) ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace
	

**## Table A10: Baseline decomposition into margin
noi di ""
noi di "***************************************************"
noi di "****************** Table A10 **********************"
noi di "***************************************************"
noi di ""
noi di "***************************"
noi di "******** Panel A **********"
noi di "***************************"
noi esttab m1*, keep(ave_ols_ch9) b(%9.3f) se(%9.3f) r2(%9.3f) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(IQR, label( "IQE") fmt( %04.3fc  %3s %10s %9.0fc  %04.3fc   %04.3fc ))

noi di ""
noi di "***************************"
noi di "******** Panel B **********"
noi di "***************************"
noi esttab m2*, keep(ave_ols_ch9) b(%9.3f) se(%9.3f) r2(%9.3f) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(IQR, label( "IQE") fmt( %04.3fc  %3s %10s %9.0fc  %04.3fc   %04.3fc ))

noi di ""
noi di "***************************"
noi di "******** Panel C **********"
noi di "***************************"
noi esttab m3*, keep(ave_ols_ch9) b(%9.3f) se(%9.3f) r2(%9.3f) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(IQR spec controls N   FSB  Weak_IV_KP,  ///
	label( "IQE" 	"Estimator" "Controls"  /// 
	"Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
	fmt( %04.3fc  %3s %10s %9.0fc  %04.3fc   %04.3fc ))

* Panel A	
esttab  m1* using "$results/table_A10_panelA.tex", ///
se(%04.3f) ///
b(%04.3f) ///
star(* .1  ** .05 *** .01) ///
stats(IQR ,  ///
label( "IQE" 	) ///
fmt( %04.3fc ) ///
) ///	
nomtitles ///
	keep(ave_ols_ch9) ///
mgroups("Panel A: `lab1' ", ///
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span})) ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace

* Panel B
esttab  m2* using "$results/table_A10_panelB.tex", ///
se(%04.3f) ///
b(%04.3f) ///
star(* .1  ** .05 *** .01) ///
stats(IQR ,  ///
label( "IQE" 	) ///
fmt( %04.3fc ) ///
) ///	
keep(ave_ols_ch9) ///
nomtitles ///
nonum ///
mgroups("Panel B: `lab2' ", ///
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span})) ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace

* Panel C
esttab  m3* using "$results/table_A10_panelC.tex", ///
se(%9.3f) ///
b(%9.3f) ///
star(* .1  ** .05 *** .01) ///
stats(IQR spec controls N   FSB  Weak_IV_KP,  ///
label( "IQE" 	"Estimator" "Controls"  /// 
"Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt( %04.3fc  %3s %10s %9.0fc  %04.3fc   %04.3fc ) ///
) ///	
keep(ave_ols_ch9) ///
nomtitles ///
nonum ///
mgroups("Panel C: `lab3' ", ///
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span})) ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace

	
}
	
*******************************************************	
**# Table 4: Robustness using 1930 IV/placebo/lagged y
*******************************************************
// qui {
**# Panel A

**## Setup
use "analysis/SIC87_regression_Dataset.dta", clear
keep if year==1979

merge 1:1 sic using "raw/swiss_1930"
assert 1==2
keep if _m == 3 
drop _m
replace ave_iv_ch9 = ave_swiss_change_1930

loc ctrl1 
loc ctrl2 `ctrl1' $tariff_controls
loc ctrl3 `ctrl2' $prod_controls
loc ctrl4 `ctrl3' $rti_controls 

loc ctrl1_lab "None"
loc ctrl2_lab "+Trade"
loc ctrl3_lab "+Prod."
loc ctrl4_lab "+Tech."

egen full = rowmiss(`ctrl4')
keep if full==0
drop full


**## Regressions

loc j = 1

eststo clear 

loc outcome ln_skill_pay_diff

ivreg2 `outcome'_ch (ave_ols_up_ch9 = ave_iv_ch9  ) `ctrl4' if year==1979, robust
gen fsamp = e(sample)

sum ave_ols_ch9,d
loc iqrx `=`r(p75)'-`r(p25)''

sum `outcome'_ch, d
loc iqry `=`r(p75)'-`r(p25)''

forval i = 1/4{	
	reg  ave_ols_ch9   ave_iv_ch9  `ctrl`i'' , robust
	loc fsb = _b[ave_iv_ch9]

	eststo m`j'`i': ivreg2 `outcome'_ch  (ave_ols_ch9  = ave_iv_ch9  )  `ctrl`i''  , robust
	eststo m`j'`i', add(Weak_IV_KP e(widstat))
	eststo m`j'`i', add(FSB        `fsb')
	loc beta = _b[ave_ols_ch9]
	loc iqe = `=(`beta'*`iqrx')'
	estadd  scalar IQR = `iqe'
	estadd loc controls "`ctrl`i'_lab'"
	estadd loc spec "IV"
}	

drop fsamp
	
		
**## Make panel A: 1930 IV
noi di ""
noi di "**************************************************"
noi di "****************** Table 4 **********************"
noi di "**************************************************"
noi di ""
noi di "***************************"
noi di "******** Panel A **********"
noi di "***************************"
noi esttab  m`j'*, keep(ave_ols_ch9) se(%04.3f) star(* .1  ** .05 *** .01) ///
	stats(IQR spec controls N   FSB  Weak_IV_KP,  ///
	label( "IQE" "Estimator" "Controls" "Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
	fmt( %04.3fc  %3s %10s %9.0fc  %04.3fc    %04.3fc ) ///
	)

esttab  m`j'* using "$results/table_4_panelA.tex", ///
se(%04.3f) ///
star(* .1  ** .05 *** .01) ///
stats(IQR spec controls N   FSB  Weak_IV_KP,  ///
label( "IQE" 	"Estimator" "Controls"  /// 
"Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt( %04.3fc  %3s %10s %9.0fc  %04.3fc    %04.3fc ) ///
) ///	
nomtitles ///
mgroups("Panel A: \$ \Delta \ln(\frac{Pay_i^{Non-Prod}}{Pay_i^{Prod}})_t \$ ", ///
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span})) ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace


**# Panels B/C

**## Setup 
use "analysis/SIC87_regression_Dataset.dta", clear
global tariff_controls  dln_pstar_vw_7279_alt rho  mfa  other_tariff_change
keep if year==1979


**## Regressions
loc outcome ln_skill_pay_diff 
loc j = 1

forval t = 1/2{

	loc pre 
	if `t'==1 loc pre lag_
	if `t'==1 loc n "B"
	if `t'==2 loc new_controls lag_`outcome'_ch
	if `t'==2 loc n "C"
	* Implicitly, t=2 => pre = ""
	* Implicitly, t=1 => new_controls = ""
	* Hence: t=1 is spec with lagged y and contemporaenous controls
	*		 t=2 is spec with contrmporaneous y and lagged y control	

	eststo clear 

	loc ctrl1   
	loc ctrl2 `ctrl1' $tariff_controls  
	loc ctrl3 `ctrl2' $prod_controls
	loc ctrl4 `ctrl3' $rti_controls 

	loc ctrl1_lab "None"
	loc ctrl2_lab "+Trade"
	loc ctrl3_lab "+Prod."
	loc ctrl4_lab "+Tech."

	egen full = rowmiss(`ctrl4')
	keep if full==0
	drop full

	ivreg2 `pre'`outcome'_ch (ave_ols_up_ch9 = ave_iv_ch9  )  `ctrl4' if year==1979, robust
	gen fsamp = e(sample)

	sum ave_ols_ch9,d
	loc iqrx `=`r(p75)'-`r(p25)''

	sum `pre'`outcome'_ch, d
	loc iqry `=`r(p75)'-`r(p25)''

	forval i = 1/4{	

		reg  ave_ols_ch9   ave_iv_ch9    `new_controls'  `ctrl`i''  if year == 1979 & fsamp==1, robust
		loc fsb = _b[ave_iv_ch9]

		eststo m`j'`i': ivreg2 `pre'`outcome'_ch  (ave_ols_ch9  = ave_iv_ch9  )  `new_controls'    `ctrl`i'' if year == 1979 & fsamp==1, robust
		eststo m`j'`i', add(Weak_IV_KP e(widstat))
		eststo m`j'`i', add(FSB        `fsb')
		loc beta = _b[ave_ols_ch9]
		loc iqe = `=(`beta'*`iqrx')'
		estadd  scalar IQR = `iqe'
		estadd loc controls "`ctrl`i'_lab'"
		estadd loc spec "IV"
	}	
drop fsamp

**## Make panel B: Lag y outcome
if `t'==1{
noi di ""
noi di "***************************"
noi di "******** Panel B **********"
noi di "***************************"
noi esttab  m`j'*, se(%04.3f) star(* .1  ** .05 *** .01) ///
	stats(IQR   N   FSB  Weak_IV_KP,  ///
	label( "IQE" 	  /// 
	"Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
	fmt( %04.3fc    %9.0fc  %04.3fc  %04.3fc ) ///
	) ///	
	keep(	ave_ols_ch9  `new_controls')

esttab  m`j'* using "$results/table_4_panel`n'.tex", ///
se(%04.3f) ///
star(* .1  ** .05 *** .01) ///
stats(IQR   N   FSB  Weak_IV_KP,  ///
label( "IQE" 	  /// 
"Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt( %04.3fc    %9.0fc  %04.3fc  %04.3fc ) ///
) ///	
keep(	ave_ols_ch9  `new_controls') /// 
nomtitles ///
mgroups("Panel B: \$ \Delta \ln(\frac{Pay_i^{Non-Prod}}{Pay_i^{Prod}})_{t-1} \$", ///
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span})) ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace
}	
		
		
**## Make panel C: Lag y control	
if `t'==2{
noi di ""
noi di "***************************"
noi di "******** Panel C **********"
noi di "***************************"
noi esttab  m`j'*, se(%04.3f) star(* .1  ** .05 *** .01) ///
	stats(IQR spec controls N   FSB  Weak_IV_KP,  ///
	label( "IQE" "Estimator" "Controls" "Obs."  "\$1^{st}\$ Stage Coeff." ///
	"KP F-Stat"   ) ///
	fmt( %04.3fc  %3s %10s %9.0fc  %04.3fc   %04.3fc   %04.3fc ) ///
	) ///	
	keep(	ave_ols_ch9  `new_controls')

esttab  m`j'* using "$results/table_4_panel`n'.tex", ///
se(%04.3f) ///
star(* .1  ** .05 *** .01) ///
stats(IQR spec controls N   FSB  Weak_IV_KP,  ///
label( "IQE" "Estimator" "Controls"  "Obs."  "\$1^{st}\$ Stage Coeff." ///
"KP F-Stat" ) ///
fmt( %04.3fc  %3s %10s %9.0fc  %04.3fc   %04.3fc   %04.3fc ) ///
) ///	
keep(	ave_ols_ch9  `new_controls') /// 
nomtitles ///
mgroups("Panel C: \$ \Delta \ln(\frac{Pay_i^{Non-Prod}}{Pay_i^{Prod}})_{t} \$", ///
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span})) ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace
}
}
	
}

	
********************************************
**# Table A2: Balance tests on 1930 tariffs
********************************************
qui { 
**# Setup
use "analysis/com_controls_clean", clear
eststo clear
destring sic4, gen(sic)
merge 1:1 sic using "raw/swiss_1930"
keep if _m == 3
drop _m

replace ave_1930 = ln(1+ave_1930)


**# Regressions
eststo: reg ave_1930 hp_per_worker_1930, robust
eststo: reg ave_1930 skilled_share_1930, robust
eststo: reg ave_1930 female_share_1930, robust
eststo: reg ave_1930 mat_ship_1930, robust
eststo: reg ave_1930 invest_1930, robust
eststo: reg ave_1930 skilled_share_1930 hp_per_worker_1930 mat_ship_1930 female_share_1930 invest_1930, robust

**# Make table
noi di ""
noi di "**************************************************"
noi di "****************** Table A2 **********************"
noi di "**************************************************"
noi esttab, stats(N r2) se(%9.3f) b(%9.3f) star(* .1  ** .05 *** .01)

esttab   using "$results/table_A2.tex", ///
se(%9.3f) ///
b(%9.3f) ///
star(* .1  ** .05 *** .01) ///
nomtitles /// 
mgroups(" \$ \Delta AVE_{i}^{1930} )\$ ", ///
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span})) ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
r2 ///
replace	
}

******************************************		
**# Table A4: Trade outcomes with col2 IV
******************************************
qui {	
**# Setup
use "analysis/SIC87_regression_Dataset.dta", clear
keep if year==1979

gen ave_c2 = tariffs_c2/imports_c2

egen full = rowmiss(ave_ols_ch9 ave_iv_ch9 ln_imp imp_pen ln_exp exp_ship   $tariff_controls )   		
keep if full==0


**# Regressions
eststo clear

sum ave_ols_ch9,d
loc iqrx `=`r(p75)'-`r(p25)''

sum ln_imp_ch, d
loc iqry `=`r(p75)'-`r(p25)''

eststo m11: reg ln_imp_ch ave_ols_ch9 if year == 1979, robust
estadd loc spec = "OLS"
eststo m11, add(UnderID_p  e(idp))
eststo m11, add(Weak_IV_KP e(widstat))

loc beta = _b[ave_ols_ch9]

loc iqe = `=(`beta'*`iqrx')'
estadd  scalar IQR = `iqe'

eststo m21: reg ln_imp_ch ave_ols_ch9 if year == 1979, robust
estadd loc spec = "OLS"
eststo m21, add(UnderID_p  e(idp))
eststo m21, add(Weak_IV_KP e(widstat))

loc beta = _b[ave_ols_ch9]

loc iqe = `=(`beta'*`iqrx')'
estadd  scalar IQR = `iqe'	

loc ctrl2 
loc ctrl3  		   dln_pstar_vw_7279_alt rho 
loc ctrl4  `ctrl3' mfa other_tariff_change
loc ctrl5  `ctrl4' 
loc ctrl6  `ctrl4' 
loc ctrl7  `ctrl4' 

loc yvar2 ln_imp
loc yvar3 ln_imp
loc yvar4 ln_imp
loc yvar5 imp_pen
loc yvar6 ln_exp
loc yvar7 exp_ship

forval i = 2/7 {
	sum `yvar`i''_ch,d
	loc iqry `=`r(p75)'-`r(p25)''
	sum ave_ols_ch9,d
	loc iqrx `=`r(p75)'-`r(p25)''

	reg ave_ols_ch9 ave_c2  `ctrl`i'' if year == 1979, robust
	loc fsb = _b[ave_c2]
	eststo m2`i': ivreg2  `yvar`i''_ch (ave_ols_ch9  = ave_c2) `ctrl`i'' if year == 1979, robust
	estadd loc spec = "IV"
	eststo m2`i', add(UnderID_p  e(idp))
	eststo m2`i', add(Weak_IV_KP e(widstat))
	eststo m2`i', add(FSB        `fsb')
	loc beta = _b[ave_ols_ch9]
	loc iqe = `=(`beta'*`iqrx')'
	estadd  scalar IQR = `iqe'		

}


**# Make table		
noi di ""
noi di "**************************************************"
noi di "****************** Table A4 **********************"
noi di "**************************************************"
noi esttab m2* , se(%9.3f) b(%9.3f) star(* .1  ** .05 *** .01) ///
	stats(IQR spec N   FSB  Weak_IV_KP, label(	 "IQE" "Estimator"  /// 
"Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt( %04.3fc   %3s %9.0fc    %04.3fc   %04.3fc ))		

esttab m2* using "$results/table_A4.tex", ///
se(%9.3f) ///
b(%9.3f) ///
star(* .1  ** .05 *** .01) ///
mtitles("$\Delta \ln(Imp_{i})\$" ///
"$\Delta \ln(Imp_{i})\$" ///
"$\Delta \ln(Imp_{i})\$" ///
"$\Delta \ln(Imp_{i})\$" ///
"$\Delta ImpPen_{i}\$" ///
"$\Delta \ln(Exp_{i})\$" /// 
"$\Delta \frac{Exp_i}{Ship_i}\$" 	///
) ///
stats(IQR spec N   FSB  Weak_IV_KP,  ///
label(	 "IQE" "Estimator"  /// 
"Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt( %04.3fc   %3s %9.0fc    %04.3fc   %04.3fc ) ///
) ///		
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace
}

******************************************
**# Table A5 Trade outcomes with 1930 IV
******************************************
qui {
**# Setup
use "analysis/SIC87_regression_Dataset.dta", clear
keep if year==1979

merge 1:1 sic using "raw/swiss_1930"
keep if _m == 3
drop _m

replace ave_iv_ch9 = ave_swiss_change_1930
egen full = rowmiss(ave_ols_ch9 ave_iv_ch9 ln_imp imp_pen ln_exp exp_ship   $tariff_controls )   		
keep if full==0

**# Regressions		
eststo clear

sum ave_ols_ch9,d
loc iqrx `=`r(p75)'-`r(p25)''

sum ln_imp_ch, d
loc iqry `=`r(p75)'-`r(p25)''

eststo m11: reg ln_imp_ch ave_ols_ch9 if year == 1979, robust
estadd loc spec = "OLS"
eststo m11, add(UnderID_p  e(idp))
eststo m11, add(Weak_IV_KP e(widstat))

loc beta = _b[ave_ols_ch9]

loc iqe = `=(`beta'*`iqrx')/(`iqry')'
estadd  scalar IQR = `iqe'

loc ctrl2 
loc ctrl3  		   dln_pstar_vw_7279_alt rho 
loc ctrl4  `ctrl3' mfa other_tariff_change 
loc ctrl5  `ctrl4' 
loc ctrl6  `ctrl4' 
loc ctrl7  `ctrl4' 

loc yvar2 ln_imp
loc yvar3 ln_imp
loc yvar4 ln_imp
loc yvar5 imp_pen
loc yvar6 ln_exp
loc yvar7 exp_ship

forval i = 2/7 {

	sum `yvar`i''_ch,d
	loc iqry `=`r(p75)'-`r(p25)''
	sum ave_ols_ch9,d
	loc iqrx `=`r(p75)'-`r(p25)''

	reg 			     ave_ols_ch9  	    ave_iv_ch9  `ctrl`i'' if year == 1979, robust
	loc fsb = _b[ave_iv_ch9]
	eststo m1`i': ivreg2  `yvar`i''_ch (ave_ols_ch9  = ave_iv_ch9) `ctrl`i'' if year == 1979, robust
	estadd loc spec = "IV"
	eststo m1`i', add(UnderID_p  e(idp))
	eststo m1`i', add(Weak_IV_KP e(widstat))
	eststo m1`i', add(FSB        `fsb')
	loc beta = _b[ave_ols_ch9]
	loc iqe = `=(`beta'*`iqrx')'
	estadd  scalar IQR = `iqe'
}
			
**# Make table
noi di ""
noi di "**************************************************"
noi di "****************** Table A5 **********************"
noi di "**************************************************"
noi esttab m1*, se(%9.3f) b(%9.3f) star(* .1  ** .05 *** .01) ///
	stats(IQR spec N   FSB  Weak_IV_KP,  ///
	label(	 "IQE" "Estimator"  /// 
	"Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
	fmt( %04.3fc   %3s %9.0fc    %04.3fc   %04.3fc ))		

esttab m1* using "$results/table_A5.tex", ///
se(%9.3f) ///
b(%9.3f) ///
star(* .1  ** .05 *** .01) ///
mtitles("$\Delta \ln(Imp_{i})\$" ///
"$\Delta \ln(Imp_{i})\$" ///
"$\Delta \ln(Imp_{i})\$" ///
"$\Delta \ln(Imp_{i})\$" ///
"$\Delta ImpPen_{i}\$" ///
"$\Delta \ln(Exp_{i})\$" /// 
"$\Delta \frac{Exp_i}{Ship_i}\$" 	///
) ///
stats(IQR spec N   FSB  Weak_IV_KP,  ///
label(	 "IQE" "Estimator"  /// 
"Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt( %04.3fc   %3s %9.0fc    %04.3fc   %04.3fc ) ///
) ///		
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace	
}
		
		
******************************************		
**# Table A6: Pay inequality with col2 IV
******************************************	
qui {
**# Setup 
use "analysis/SIC87_regression_Dataset.dta", clear
global tariff_controls dln_pstar_vw_7279_alt rho  mfa  other_tariff_change
keep if year==1979

gen ave_c2 = tariffs_c2/imports_c2


**# Run regressions
loc outcome ln_skill_pay_diff
loc j = 1
	
eststo clear 

loc ctrl1   
loc ctrl2 `ctrl1' $tariff_controls  
loc ctrl3 `ctrl2' $prod_controls 
loc ctrl4 `ctrl3' $rti_controls

loc ctrl1_lab "None"
loc ctrl2_lab "+Trade"
loc ctrl3_lab "+Prod."
loc ctrl4_lab "+Tech."

egen full = rowmiss(`ctrl4')
keep if full==0
drop full

ivreg2 `outcome'_ch (ave_ols_ch9 = ave_iv_ch9  )  `ctrl4' if year==1979, robust
gen fsamp = e(sample)

sum ave_ols_ch9,d
loc iqrx `=`r(p75)'-`r(p25)''

sum `outcome'_ch, d
loc iqry `=`r(p75)'-`r(p25)''

forval i = 1/4{	

	reg  ave_ols_ch9   ave_c2 `new_controls'  `ctrl`i''  if year == 1979 & fsamp==1, robust
	loc fsb = _b[ave_c2]

	eststo m`j'`i': ivreg2 `outcome'_ch  (ave_ols_ch9  = ave_c2  )  `ctrl`i'' if year == 1979 & fsamp==1, robust
	eststo m`j'`i', add(UnderID_p  e(idp))
	eststo m`j'`i', add(Weak_IV_KP e(widstat))
	eststo m`j'`i', add(FSB        `fsb')
	loc beta = _b[ave_ols_ch9]
	loc iqe = `=(`beta'*`iqrx')'
	estadd  scalar IQR = `iqe'
	estadd loc controls "`ctrl`i'_lab'"
	estadd loc spec "IV"
}	
drop fsamp
		 

**# Make table
noi di ""
noi di "**************************************************"
noi di "****************** Table A6 **********************"
noi di "**************************************************"
noi esttab  m`j'*, keep(ave_ols_ch9) se(%04.3f) star(* .1  ** .05 *** .01) ///
	stats(IQR spec controls N   FSB  Weak_IV_KP,  ///
	label( "IQE" 	"Estimator" "Controls"  /// 
	"Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
	fmt( %04.3fc  %3s %10s %9.0fc  %04.3fc   %04.3fc ))

esttab  m`j'* using "$results/table_A6.tex", ///
se(%04.3f) ///
star(* .1  ** .05 *** .01) ///
stats(IQR spec controls N   FSB  Weak_IV_KP,  ///
label( "IQE" 	"Estimator" "Controls"  /// 
"Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt( %04.3fc  %3s %10s %9.0fc  %04.3fc   %04.3fc ) ///
) ///	
keep(	ave_ols_ch9   ) /// 
nomtitles ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace
	
}

	
******************************************************	
**# Table A7: Trade outcomes with lagged y as control	
******************************************************

qui{
**# Setup
use "analysis/SIC87_regression_Dataset.dta", clear
keep if year==1979

egen full = rowmiss(ave_ols_ch9 ave_iv_ch9 ln_imp imp_pen ln_exp exp_ship   $tariff_controls )   		
keep if full==0

**# Regressions		
eststo clear

sum ave_ols_ch9,d
loc iqrx `=`r(p75)'-`r(p25)''

sum ln_imp_ch, d
loc iqry `=`r(p75)'-`r(p25)''

eststo m11: reg ln_imp_ch ave_ols_ch9 if year == 1979, robust
estadd loc spec = "OLS"
eststo m11, add(UnderID_p  e(idp))
eststo m11, add(Weak_IV_KP e(widstat))

loc beta = _b[ave_ols_ch9]

loc iqe = `=(`beta'*`iqrx')/(`iqry')'
estadd  scalar IQR = `iqe'

loc ctrl2 
loc ctrl3  		   dln_pstar_vw_7279_alt rho 
loc ctrl4  `ctrl3' mfa  other_tariff_change
loc ctrl5  `ctrl4' 
loc ctrl6  `ctrl4' 
loc ctrl7  `ctrl4' 

loc yvar2 ln_imp
loc yvar3 ln_imp
loc yvar4 ln_imp
loc yvar5 imp_pen
loc yvar6 ln_exp
loc yvar7 exp_ship

forval i = 2/7 {

	sum `yvar`i''_ch,d
	loc iqry `=`r(p75)'-`r(p25)''
	sum ave_ols_ch9,d
	loc iqrx `=`r(p75)'-`r(p25)''

	reg ave_ols_ch9 ave_iv_ch9 lag_`yvar`i''_ch `ctrl`i'' if year == 1979, robust
	loc fsb = _b[ave_iv_ch9]
	eststo m1`i': ivreg2  `yvar`i''_ch (ave_ols_ch9  = ave_iv_ch9)  lag_`yvar`i''_ch `ctrl`i'' if year == 1979, robust
	estadd loc spec = "IV"
	eststo m1`i', add(UnderID_p  e(idp))
	eststo m1`i', add(Weak_IV_KP e(widstat))
	eststo m1`i', add(FSB        `fsb')
	loc beta = _b[ave_ols_ch9]
	loc iqe = `=(`beta'*`iqrx')'
	estadd  scalar IQR = `iqe'
}

**# Make Table
noi di ""
noi di "**************************************************"
noi di "****************** Table A7 **********************"
noi di "**************************************************"
noi esttab m1*, keep(ave_ols_ch9 lag_*_ch) se(%9.3f) b(%9.3f) star(* .1  ** .05 *** .01) ///
	stats(IQR spec N   FSB  Weak_IV_KP,  ///
	label(	 "IQE" "Estimator"  /// 
	"Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
	fmt( %04.3fc   %3s %9.0fc    %04.3fc   %04.3fc ))

esttab m1* using "$results/table_A7.tex", ///
se(%9.3f) ///
b(%9.3f) ///
star(* .1  ** .05 *** .01) ///
mtitles("$\Delta \ln(Imp_{i})\$" ///
"$\Delta \ln(Imp_{i})\$" ///
"$\Delta \ln(Imp_{i})\$" ///
"$\Delta \ln(Imp_{i})\$" ///
"$\Delta ImpPen_{i}\$" ///
"$\Delta \ln(Exp_{i})\$" /// 
"$\Delta \frac{Exp_i}{Ship_i}\$" 	///
) ///
stats(IQR spec N   FSB  Weak_IV_KP,  ///
label(	 "IQE" "Estimator"  /// 
"Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt( %04.3fc   %3s %9.0fc    %04.3fc   %04.3fc ) ///
) ///		
booktabs ///
substitute(\_ _) ///
keep(ave_ols_ch9 lag_*_ch) ///
label ///
nonotes /// 
replace	
}


************************************************
**# Table A8: Trade outcomes & upstream tariffs
************************************************
qui {	
**# Setup
use "analysis/SIC87_regression_Dataset.dta", clear
keep if year==1979

egen full = rowmiss(ave_ols_ch9 ave_iv_ch9 ln_imp imp_pen ln_exp exp_ship   $tariff_controls )   		
keep if full==0

**# Regressions
sum ave_ols_ch9,d
loc iqrx `=`r(p75)'-`r(p25)''
sum ave_ols_up_ch9,d
loc iqrx_up `=`r(p75)'-`r(p25)''
sum ln_imp_ch, d
loc iqry `=`r(p75)'-`r(p25)''

eststo m11: reg `pre'ln_imp_ch ave_ols_ch9 ave_ols_up_ch9 if year == 1979, robust
estadd loc spec = "OLS"
eststo m11, add(UnderID_p  e(idp))
eststo m11, add(Weak_IV_KP e(widstat))

loc beta = _b[ave_ols_ch9]
loc beta_up = _b[ave_ols_up_ch9]
loc iqe = `=(`beta'*`iqrx')' 
estadd  scalar IQR = `iqe'

loc iqe_up = `=(`beta_up'*`iqrx')'
estadd  scalar IQR_up = `iqe_up'

loc ctrl2 
loc ctrl3  		   dln_pstar_vw_7279_alt rho 
loc ctrl4  `ctrl3' mfa  other_tariff_change
loc ctrl5  `ctrl4' 
loc ctrl6  `ctrl4' 
loc ctrl7  `ctrl4' 

loc yvar2 ln_imp
loc yvar3 ln_imp
loc yvar4 ln_imp
loc yvar5 imp_pen
loc yvar6 ln_exp
loc yvar7 exp_ship

forval i = 2/7 {

	sum `yvar`i''_ch,d
	loc iqry `=`r(p75)'-`r(p25)''
	sum ave_ols_ch9,d
	loc iqrx `=`r(p75)'-`r(p25)''

	sum ave_ols_up_ch9,d
	loc iqrx_up `=`r(p75)'-`r(p25)''

	reg ave_ols_ch9 ave_iv_ch9  `ctrl`i'' if year == 1979, robust
	loc fsb = _b[ave_iv_ch9]
	eststo m1`i': ivreg2  `yvar`i''_ch (ave_ols_ch9 ave_ols_up_ch9  = ave_iv_ch9 ave_iv_up_ch9) `ctrl`i'' if year == 1979, robust
	estadd loc spec = "IV"
	eststo m1`i', add(UnderID_p  e(idp))
	eststo m1`i', add(Weak_IV_KP e(widstat))
	eststo m1`i', add(FSB        `fsb')
	loc beta 	= _b[ave_ols_ch9]
	loc beta_up = _b[ave_ols_up_ch9]
	loc iqe = `=(`beta'*`iqrx')'
	estadd  scalar IQR = `iqe'
	loc iqe_up = `=(`beta_up'*`iqrx_up')'
	estadd  scalar IQR_up = `iqe_up'
}

**# Make table		
noi di ""
noi di "**************************************************"
noi di "****************** Table A8 **********************"
noi di "**************************************************"
noi	esttab m1*, se(%9.3f) b(%9.3f) star(* .1  ** .05 *** .01) ///
	stats(IQR IQR_up spec N  FSB    Weak_IV_KP,  ///
	label(	 "IQE" "IQE\$^{up}\$" "Estimator"  /// 
	"Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
	fmt( %04.3fc %04.3fc    %3s %9.0fc    %04.3fc   %04.3fc ))		

esttab m1* using "$results/table_A8.tex", ///
se(%9.3f) ///
b(%9.3f) ///
star(* .1  ** .05 *** .01) ///
mtitles("$\Delta \ln(Imp_{i})\$" ///
"$\Delta \ln(Imp_{i})\$" ///
"$\Delta \ln(Imp_{i})\$" ///
"$\Delta \ln(Imp_{i})\$" ///
"$\Delta ImpPen_{i}\$" ///
"$\Delta \ln(Exp_{i})\$" /// 
"$\Delta \frac{Exp_i}{Ship_i}\$" 	///
) ///
stats(IQR IQR_up spec N  FSB    Weak_IV_KP,  ///
label(	 "IQE" "IQE\$^{up}\$" "Estimator"  /// 
"Obs."  "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt( %04.3fc %04.3fc    %3s %9.0fc    %04.3fc   %04.3fc ) ///
) ///		
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace	
}

************************************************
**# Table A9: Pay inequality & upstream tariffs
************************************************
qui{	
**# Setup	

use "analysis/SIC87_regression_Dataset.dta", clear

global tariff_controls  dln_pstar_vw_7279_alt rho  mfa   other_tariff_change
keep if year==1979

loc ctrl1_lab "None"
loc ctrl2_lab "+Trade"
loc ctrl3_lab "+Prod."
loc ctrl4_lab "+Tech."

keep if year==1979

eststo clear 


**# Regressions 
loc outcome ln_skill_pay_diff 
loc j=1

loc ctrl1 
loc ctrl2 `ctrl1' $tariff_controls
loc ctrl3 `ctrl2' $prod_controls
loc ctrl4 `ctrl3' $rti_controls

egen full = rowmiss(`ctrl4')
keep if full==0
drop full

ivreg2 `outcome'_ch_9 (ave_ols_ch9 ave_ols_up_ch9 = ave_iv_ch9 ave_iv_up_ch9) `ctrl4' if year==1979, robust
gen fsamp = e(sample)

sum ave_ols_ch9,d
loc iqrx `=`r(p75)'-`r(p25)''

sum `outcome', d
loc iqry `=`r(p75)'-`r(p25)''
sum ave_ols_up_ch9,d
loc iqrx_up `=`r(p75)'-`r(p25)''

forval i = 1/4{	
	reg   ave_ols_ch9   ave_iv_ch9  `ctrl`i'' if year == 1979 & fsamp==1, robust
	loc fsb = _b[ave_iv_ch9]
	eststo m`j'`i': ivreg2 `outcome'_ch_9  (ave_ols_ch9 ave_ols_up_ch9 = ave_iv_ch9 ave_iv_up_ch9)  `ctrl`i'' if year == 1979 & fsamp==1, robust
	eststo m`j'`i', add(UnderID_p  e(idp))
	eststo m`j'`i', add(Weak_IV_KP e(widstat))
	eststo m`j'`i', add(FSB        `fsb')
	loc beta = _b[ave_ols_ch9]
	loc beta_up = _b[ave_ols_up_ch9]
	loc iqe = `=(`beta'*`iqrx')'
	loc iqe_up = `=(`beta_up'*`iqrx_up')'
	estadd  scalar IQR = `iqe'
	estadd  scalar IQR_up = `iqe_up'
	estadd loc spec "IV"
	estadd loc controls "`ctrl`i'_lab'"
}	
drop fsamp
		
**# Make table		
noi di ""
noi di "**************************************************"
noi di "****************** Table A9 ***********************"
noi di "**************************************************"
noi esttab  m`j'*, keep(ave_ols_ch9 ave_ols_up_ch9) se(%04.3f) star(* .1  ** .05 *** .01) ///
stats(IQR IQR_up spec controls N Weak_IV_KP,  ///
label( "IQE" "IQE\$^{up}\$"	/// 
"Estimator" "Controls"  "Obs."   "KP F-Stat" ) ///
fmt( %04.3fc   %04.3fc %3s %10s %9.0fc   %04.3fc ))

esttab  m`j'* using "$results/table_A9.tex", ///
se(%04.3f) ///
star(* .1  ** .05 *** .01) ///
stats(IQR IQR_up spec controls N Weak_IV_KP,  ///
label( "IQE" "IQE\$^{up}\$"	/// 
"Obs."   "KP F-Stat" "Estimator" "Controls"  ) ///
fmt( %04.3fc   %04.3fc %3s %10s %9.0fc   %04.3fc ) ///
) ///	
nomtitles ///
mgroups(" \$ \Delta \ln(\frac{Pay_i^{Non-Prod}}{Pay_i^{Prod}}) \$ ", ///
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span})) ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
keep(ave_ols_ch9 ave_ols_up_ch9) ///
replace

}

*********************************************	 
**# Fig 4: Pay inequality margins & horizons
*********************************************
qui {	
**# Setup
	
use "analysis/SIC87_regression_Dataset.dta", clear
global tariff_controls  dln_pstar_vw_7279_alt rho  mfa  other_tariff_change 
keep if year==1979
eststo clear 
			
**# Regressions

foreach yvar in ln_skill_pay_diff  ln_skill_emp_diff  ln_wsp  {  // 
	foreach var in beta se lb01 lb05 lb10 ub01 ub05 ub10{
		replace `var' = .
	}

	loc i = 4
	loc ctrl1 
	loc ctrl2 `ctrl1' $tariff_controls
	loc ctrl3 `ctrl2' $prod_controls
	loc ctrl4 `ctrl3' $rti_controls

	egen fullsamp = rowmiss(`ctrl4')
	keep if fullsamp==0
	drop fullsamp
	
	preserve
		forvalues window =1/9 {
			ivreghdfe `yvar'_ch_`window' (ave_ols_ch9 = ave_iv_ch9 ) `ctrl`i'' if year == 1979 , a(year) robust

			replace beta = _b[ave_ols_ch9] if _n == `window'
			replace se   = _se[ave_ols_ch9] if _n == `window'
			replace lb01 = _b[ave_ols_ch9]-invt(e(df_r), .005)*_se[ave_ols_ch9] if _n == `window'
			replace ub01 = _b[ave_ols_ch9]+invt(e(df_r), .005)*_se[ave_ols_ch9] if _n == `window'
			replace lb05 = _b[ave_ols_ch9]-invt(e(df_r), .025)*_se[ave_ols_ch9] if _n == `window'
			replace ub05 = _b[ave_ols_ch9]+invt(e(df_r), .025)*_se[ave_ols_ch9] if _n == `window'
			replace lb10 = _b[ave_ols_ch9]-invt(e(df_r), .05)*_se[ave_ols_ch9] if _n == `window'
			replace ub10 = _b[ave_ols_ch9]+invt(e(df_r), .05)*_se[ave_ols_ch9] if _n == `window'
		}
					
**# Make graphs
					
			keep if _n<10

			keep beta lb* ub* 
			gen year = _n + 1979	

			tw rarea ub01 lb01 year , fc(gs2%10) lp(solid) lc(black) lw(thin) ///
			|| rarea ub05 lb05 year , fc(gs2%10) lp(longdash)  lc(black) lw(medthin) ///
			|| rarea ub10 lb10 year , fc(gs2%10) lp(shortdash) lc(black) lw(medium)  /// 
			|| scatter beta year , m(d) msize(large) mfcolor(white)  mc(gs2) ///
			yline(0, lp(solid))  ///
			legend(order(1 "99% CI" 2 "95% CI" 3 "90% CI" 4 "Point Esimate") pos(6) rows(1) size(large)) ///
			xlabel(1980(1)1988, labsize(large)) ///
			xtitle("") ytitle("Coefficient on {&Delta}ln(1+AVE)", size(large)) ///
			ylabel(, labsize(large))
			graph export "$figures/fig_4_`yvar'.png", replace width(1200) height(600)
		restore
				
					
		}

} 
		
***********************************
**# Fig A2: 1978 col2 vs 1930 col1
***********************************
qui {		
**# Setup
use "analysis/SIC87_regression_Dataset.dta" if year==1979 | year == 1988, clear
bys sic (year) : gen ord = _n
xtset sic ord


foreach v in  year ln_imp ave_ols ave_iv_swiss ave_swiss ln_skill_emp_diff ln_skill_pay_diff ln_wsp {  
gen d_`v' = f.`v'-`v'
gen `v'_t0 = `v' if year==1979
}
keep if year==1979 
keep sic ave_iv_swiss_t0 imports

mmerge sic using "raw/swiss_1930.dta", type(1:1)
keep if _m==3
keep sic ave_iv_swiss_t0 ave_1930 imports
	
format ave_iv_swiss_t0 ave_1930 %03.2fc

**# Make graph
twoway scatter ave_iv_swiss_t0 ave_1930, mlc(gs2) ///
|| lfitci  ave_iv_swiss_t0 ave_1930, fc(gs2%20) clcolor(green%60) ///
|| scatter ave_1930 ave_1930 if inrange(ave_1930, 0,0.7) & inrange(ave_iv_swiss_t0, 0,0.7), m(i) c(l) lc(red%40)  ///
xtitle("1930 Smoot-Hawley AVE Tariff") ///
ytitle("1978 Column 2 AVE Tariff") ///
xlabel(0[.25]1.25) ///
ylabel(0[.25].75) ///
mlcolor(black) ///
graphregion(margin(2 2 8 2)) ///
graphregion(margin(2 2 8 2)) ///
legend(off)

graph export "$figures/fig_A2.png", replace
}

**************************************
**# Fig A3: 1930 controls persistence
**************************************
qui {
**# Setup
use "analysis/SIC87_regression_Dataset.dta", clear

tostring sic, gen(sic4)

merge m:1 sic4 using "analysis/com_controls_clean"
drop if _m == 2
drop _m

keep if year==1979

egen full = rowmiss($tariff_controls $prod_controls $rti_controls ave_ols_ch9 ave_iv_ch9  $controls_1930 )
keep if full==0
drop full 

format skilled_share_1930 hp_per_worker_1930 mat_ship_1930 female_share_1930 ///
mat_ship_def_78  ln_cap_lab_78 skill_emp_share_78 women_share %03.2fc

**# Make graph panels

* Panel a
twoway scatter mat_ship_def_78 mat_ship_1930, mlc(gs2) ///
|| lfitci  mat_ship_def_78 mat_ship_1930 , fc(gs2%20) clcolor(green%60) ///
xtitle("1929", size(small)) ///
ytitle("1978", size(small)) ///
xlabel(, labsize(small)) ///
ylabel(, labsize(small)) ///
mlcolor(black) ///
caption("(a) Materials/Shipments", position(6) justification(center)  size(small)) ///
graphregion(margin(2 2 8 2)) ///
legend(off) ///
name(matship, replace)

* Panel b
twoway scatter ln_cap_lab_78 hp_per_worker_1930, mlc(gs2) ///
|| lfitci  ln_cap_lab_78 hp_per_worker_1930, fc(gs2%20) clcolor(green%60) ///
xtitle("1929", size(small)) ///
ytitle("1978", size(small)) ///
xlabel(, labsize(small)) ///
ylabel(, labsize(small)) ///
mlcolor(black) ///
graphregion(margin(2 2 8 2)) ///
caption("(b) Log Capital/Labor", position(6) justification(center)  size(small)) ///
graphregion(margin(2 2 8 2)) ///
legend(off) ///
name(caplab, replace)

* Panel c
twoway scatter skill_emp_share_78 skilled_share_1930, mlc(gs2) ///
|| lfitci  skill_emp_share_78 skilled_share_1930, fc(gs2%20) clcolor(green%60) ///
xtitle("1929", size(small)) ///
ytitle("1978", size(small)) ///
xlabel(, labsize(small)) ///
ylabel(, labsize(small)) ///
caption("(c) Non-production Employment Share", position(6) justification(center) size(small)) ///
mlcolor(black) ///
graphregion(margin(2 2 8 2)) ///
legend(off) ///
name(skillemp, replace)

* Panel d
twoway scatter women_share female_share_1930, mlc(gs2) ///
|| lfitci  women_share female_share_1930, fc(gs2%20) clcolor(green%60) ///
xtitle("1929", size(small)) ///
ytitle("1978", size(small)) ///
xlabel(, labsize(small)) ///
ylabel(, labsize(small)) ///
caption("(d) Women's Employment Share", position(6) justification(center) size(small)) ///
mlcolor(black) ///
graphregion(margin(2 2 8 2)) ///
legend(off) ///
name(womens, replace)

* Panel e 
twoway scatter ln_invest_def_78 invest_1930, mlc(gs2) ///
|| lfitci  ln_invest_def_78 invest_1930, fc(gs2%20) clcolor(green%60) ///
xtitle("1929", size(small)) ///
ytitle("1978", size(small)) ///
xlabel(, labsize(small)) ///
ylabel(, labsize(small)) ///
mlcolor(black) ///
caption("(e) Log Investment", position(6) justification(center)  size(small)) ///
graphregion(margin(2 2 8 2)) ///
legend(off) ///
name(invest, replace)

**# Combine graph panels
graph combine caplab skillemp womens matship invest, rows(3) ///
imargin(small) ///
    xsize(6) ysize(7.5) ///
    graphregion(margin(2 2 2 2)) 
graph export "$figures/fig_A3.png", replace 

}
***************************************
**# Fig A4: Trade outocmes by horizons
***************************************
qui {
**# Setup
	
use "analysis/SIC87_regression_Dataset.dta", clear

foreach yvar in  ln_imp imp_pen exp_ship {  // 
foreach var in beta se lb01 lb05 lb10 ub01 ub05 ub10{
replace `var' = .
}

loc i = 5
	loc ctry1  
	loc ctrl2  `ctrl1' dln_pstar_vw_7279_alt 
	loc ctrl3  `ctrl2' rho 
	loc ctrl4  `ctrl3' mfa
	loc ctrl5  `ctrl4' 

	forvalues window =1/9 {

		ivreghdfe `yvar'_ch_`window' (ave_ols_ch`window' = ave_iv_ch`window' ) `ctrl`i'' if year == 1979 , a(year) robust

		replace beta = _b[ave_ols_ch`window'] if _n == `window'
		replace se = _se[ave_ols_ch`window'] if _n == `window'
		replace lb01 = _b[ave_ols_ch`window']-invt(e(df_r), .005)*_se[ave_ols_ch`window'] if _n == `window'
		replace ub01 = _b[ave_ols_ch`window']+invt(e(df_r), .005)*_se[ave_ols_ch`window'] if _n == `window'
		replace lb05 = _b[ave_ols_ch`window']-invt(e(df_r), .025)*_se[ave_ols_ch`window'] if _n == `window'
		replace ub05 = _b[ave_ols_ch`window']+invt(e(df_r), .025)*_se[ave_ols_ch`window'] if _n == `window'
		replace lb10 = _b[ave_ols_ch`window']-invt(e(df_r), .05)*_se[ave_ols_ch`window'] if _n == `window'
		replace ub10 = _b[ave_ols_ch`window']+invt(e(df_r), .05)*_se[ave_ols_ch`window'] if _n == `window'

}
				
**# Make graph

preserve 
keep if _n<10

keep beta lb* ub* 
gen year = _n + 1979	

tw rarea ub01 lb01 year , fc(gs2%05) lp(solid) lc(green) lw(thin) ///
|| rarea ub05 lb05 year , fc(gs2%05) lp(dash)  lc(purple) lw(medthin) ///
|| rarea ub10 lb10 year , fc(gs2%05) lp(dash_dot) lc(red) lw(medium)  /// 
|| scatter beta year , m(d) msize(large) mfcolor(white)  mc(gs2) ///
yline(0, lp(solid))  ///
legend(order(1 "99% CI" 2 "95% CI" 3 "90% CI" 4 "Point Esimate") pos(6) rows(1) size(large)) ///
xlabel(1980(1)1988, labsize(large)) ylabel(, labsize(large)) ///
xtitle("") ytitle("Coefficient on {&Delta}ln(1+AVE)", size(large))

graph export "$figures/fig_A4_`yvar'.png", replace
restore 

}
}


**************************************	
**# Fig B1: Tokyo foreign tariff cuts 
************************************** 	
qui {
**# Setup

*SIC87 import, export data from Peter Schott
*https://faculty.som.yale.edu/peterschott/international-trade-data/
use "raw/xm_sic87_72_105_20120424.dta", clear
keep if year==1979
keep if sic>=2000 & sic < 4000

collapse (sum) customs x, by(wbcode)
// 	fra
// 	ita 
// 	deu
// 	nld
// 	bel
// 	dnk
// 	ire
// 	gbr

gen eu = inlist(wbc, "FRA", "ITA", "DEU", "LUX", "NLD", "BEL", "DNK", "IRL", "GBR")
gen group = wbcode
replace group = "EEC" if eu==1

gen have= inlist(group, "EEC", "JPN", "CAN")
collapse (sum) x, by(group)
gsort - x
replace group = "ROW" if _n>20
gen rank = _n
replace rank = 21 if _n>20
collapse (sum) x, by(group rank)
sort rank
egen t=total(x)
gen xshare = x/t
gen have = rank<4
format xsh %03.2fc

**# Make graph
tw scatter xsh rank, mlab(group) mlabangle(45) mlabpos(1) mlabc(gs2%40) mc(gs2%40) || scatter xsh rank if have==1, mlab(group) mlabangle(45) mlabpos(1) ytitle("Share") xtitle("Destination Country Rank") m(Dh) msize(medium) legend(order (1 "Destination Country " 2 "Foreign Tariff Sample") row(1) pos(6))
graph export "$figures/fig_B1.png", replace
} 
  
**************************************  
**# Fig B2: US vs foreign tariff cuts
**************************************
qui {
**# Setup
use "analysis/SIC87_regression_Dataset.dta", clear
keep if year==1979
loc ctrl1 
loc ctrl2 `ctrl1' $tariff_controls
loc ctrl3 `ctrl2' $prod_controls
loc ctrl4 `ctrl3' $rti_controls

egen full = rowmiss(`ctrl4')
keep if full==0
drop full
replace other_tariff_change = other_tariff_change/9
format other_tariff_change ave_ols_ch9 ave_iv_ch9 %03.2fc


**# Make graphs
tw scatter other_tariff_change  ave_ols_ch9 if other_tariff_change <2/9, mlc(gs2) ///
|| lfitci  other_tariff_change  ave_ols_ch9 , fc(gs2%20) clcolor(green%60) ///
ytitle("{&Delta} AVE{sup:Foreign}")  ///
xtitle("{&Delta}ln(1+AVE{sup:US})") ///
legend(off)
graph export "$figures/fig_B2.png", replace 
}


*********************
**# Fig D1 & D2: BBG   
*********************
  
qui { 
**# Setup 
   
* Notation
* y_it    = pay_i/pay     = ind i share of US pay
* y_NP_t  = pay_NP/pay    = skilled share of US income
* y_NP_it = payNP_i/pay_i = ind i skilled pay as share of ind i pay
* t could be year or could be "hat" to indicate predicted value based on initial value plus regression based predicted change

* Get relevant data
clear 
tempfile bbg
save `bbg', emptyok
matrix define c = J(9,17,.)

forval ch = 1/9{
	
loc fy `=79+`ch'' 
use "analysis/SIC87_regression_Dataset.dta", clear
matrix c[`ch',1]=`fy'

global controls $prod_controls $rti_controls $tariff_controls

keep sic year $controls pay skill_pay ave_ols_ch`ch' ave_iv_ch`ch' ln_skill_pay_diff_ch_`ch'
keep if inlist(year,1979,19`fy')


* Drop obs that don't have all data needed for regressions
egen fullsamp = rowmiss(ave_ols_ch`ch'  ave_iv_ch`ch' $controls)
bys sic : egen keepme = max((fullsamp==0)*(year==1979))
unique sic if keepme==1
assert missing(other_tariff_change) | missing(women_share) if keepme==0 & year==1979
* 4 inds don't have export tariffs
* 5 inds don't have 1970 womens share
keep if keepme==1
drop keepme fullsamp


* Generate vars
gen          pay_it    				= pay  
bys year: 	 egen pay_t    			= total(pay_it) 
gen			 y_it					= pay_it/pay_t  // s
assert 		 inrange(y_it,0,1)
sort sic year

gen		     skill_pay_it			= skill_pay   
gen 		 yNP_it					= skill_pay_it/pay_it // p
assert 		 inrange(yNP_it,0,1)

gen 	     skill_pay_diff_it		= (pay_it-skill_pay_it)/skill_pay_it 
* Note: This is pay_unskilled / pay_skilled
* By construction, regression coefficient on delta(ln(x)) is negative of the coefficient on delta(ln(x^-1))

bys year:	 egen 	skill_pay_t		   = total(skill_pay_it)
gen 		 		yNP_t  = skill_pay_t/pay_t
gen 		 		yNP_79 = yNP_t if year==1979
bys sic: ereplace 	yNP_79 = min(yNP_79)
gen 		 		yNP_`fy' = yNP_t if year==19`fy'
bys sic: ereplace 	yNP_`fy' = min(yNP_`fy')

drop yNP_t skill_pay_t
sum yNP_*
* 1979 35.7%
* 1988 41.8%
* Change in economy-wide skill pay share is about 6% pts (16%)

sort sic year
bys sic: gen d_ln_pay_it 			= (ln(pay_it[2])-ln(pay_it[1]))
bys sic: gen d_ln_skill_pay_diff_it = (ln(skill_pay_diff_it[2])-ln(skill_pay_diff_it[1]))
keep if year==1979

**# Regressions
* Get prediced change in ind i share of US pay (d_ln_y_i_hat)
eststo acr_sh`ch':		ivreghdfe	  d_ln_pay_it (ave_ols_ch`ch' = ave_iv_ch`ch') $controls, robust abs(year)

ereturn list
matrix c[`ch',2] = _b[ave_ols_ch`ch'] 
matrix c[`ch',3]    = _se[ave_ols_ch`ch'] 
matrix c[`ch',4]  = _b[ave_ols_ch`ch']-invt(e(df_r), .005)*_se[ave_ols_ch`ch'] 
matrix c[`ch',5]  = _b[ave_ols_ch`ch']+invt(e(df_r), .005)*_se[ave_ols_ch`ch'] 
matrix c[`ch',6]  = _b[ave_ols_ch`ch']-invt(e(df_r), .025)*_se[ave_ols_ch`ch'] 
matrix c[`ch',7]  = _b[ave_ols_ch`ch']+invt(e(df_r), .025)*_se[ave_ols_ch`ch'] 
matrix c[`ch',8] = _b[ave_ols_ch`ch']-invt(e(df_r), .05 )*_se[ave_ols_ch`ch'] 
matrix c[`ch',9]  = _b[ave_ols_ch`ch']+invt(e(df_r), .05 )*_se[ave_ols_ch`ch'] 

gen       d_ln_pay_i_hat	= ave_ols_ch`ch'*_b[ave_ols_ch`ch']
gen 	  d_pay_i_hat       = pay_it*(exp(d_ln_pay_i_hat)-1)
* See derivation of equations 9-10 in BBG.tex on overleaf
bys year: egen d_pay_t_hat	= total(d_pay_i_hat)
gen		  y_i_hat			= (pay_it + d_pay_i_hat)/(pay_t + d_pay_t_hat)
* This is pay_i_hat/pay_t_hat
gen		  d_y_i_hat			= y_i_hat - y_it
assert    inrange(y_i_hat,0,1)
* Checking that alpha_i,t+1 between 0 and 1

* Get prediced change in ind i skilled share of ind i pay (d_ln_y_i_hat)
eststo wi_sh`ch':	ivreghdfe	 d_ln_skill_pay_diff_it (ave_ols_ch`ch' = ave_iv_ch`ch') $controls, robust   abs(year)

matrix c[`ch',10]  = _b[ave_ols_ch`ch'] 
matrix c[`ch',11]  = _se[ave_ols_ch`ch'] 
matrix c[`ch',12]  = _b[ave_ols_ch`ch']-invt(e(df_r), .005)*_se[ave_ols_ch`ch'] 
matrix c[`ch',13]  = _b[ave_ols_ch`ch']+invt(e(df_r), .005)*_se[ave_ols_ch`ch'] 
matrix c[`ch',14]  = _b[ave_ols_ch`ch']-invt(e(df_r), .025)*_se[ave_ols_ch`ch'] 
matrix c[`ch',15]  = _b[ave_ols_ch`ch']+invt(e(df_r), .025)*_se[ave_ols_ch`ch'] 
matrix c[`ch',16]  = _b[ave_ols_ch`ch']-invt(e(df_r), .05 )*_se[ave_ols_ch`ch'] 
matrix c[`ch',17]  = _b[ave_ols_ch`ch']+invt(e(df_r), .05 )*_se[ave_ols_ch`ch'] 

gen    d_ln_skill_pay_diff_i_hat	= ave_ols_ch`ch'*_b[ave_ols_ch`ch']
gen    d_skill_pay_diff_i_hat       = skill_pay_diff_it*(exp(d_ln_skill_pay_diff_i_hat)-1)
gen    d_yNP_i_hat	     			= -d_skill_pay_diff_i_hat/((1+skill_pay_diff_it+d_skill_pay_diff_i_hat)*(1+skill_pay_diff_it))
* See derivation of equation 12 in BBG.tex on overleaf
sum    d_yNP_i_hat, d
* Mean and median around 1.2% to 1.3% points which is around 20% of economy wide-change
gen    yNP_i_hat = yNP_it + d_yNP_i_hat
assert inrange(yNP_i_hat,0,1)
* Checking that y_i,t+1 between 0 and 1


**# Aggregate changes
* Aggregate changes into economy-wide decomposition
keep year sic y* d_y* 

egen d_yNP_bw 		= total(d_y_i_hat *   yNP_it)   // 
egen d_yNP_within 	= total(  y_it    * d_yNP_i_hat)  // 
egen d_yNP_int	    = total(d_y_i_hat * d_yNP_i_hat)  // 
gen  d_yNP_int_d2   =  d_yNP_int/2
replace d_yNP_bw 	=  d_yNP_bw  + d_yNP_int_d2 
replace d_yNP_within 	=  d_yNP_within  + d_yNP_int_d2 

gen  d_yNP_total 	= d_yNP_within + d_yNP_bw  
gen  share 		    = d_yNP_total/(yNP_`fy' - yNP_79) 

gen share_bw     = d_yNP_bw     / d_yNP_total
gen share_within = d_yNP_within / d_yNP_total

**# Make figures
* Keep results for table
keep share*
duplicates drop
gen year = 19`fy'
append using `bbg'
save `bbg', replace

}
 
**## Fig D1 
use `bbg', clear

gen tshare = share
gen tshare_wi  = share_within*share 
gen tshare_bw = share*share_bw
format tsh* %03.2fc

tsline tshare tshare_wi tshare_bw, lw(medthick medthick medthick) lc(gs2%40 blue%40 green%80) lp(solid dash dash_dot) ///	
yline(0, lp(solid))  ///
xlabel(1980(1)1988) ///
xtitle("Year") ///
ytitle("Share") ylabel(0(.05).3)  ///
legend(order(1 "Share of Observed" 2 "Within Industry" 3 "Between Industry" ) row(1) pos(6)) 
graph export "$figures/fig_D1.png", replace width(1200) height(800)

		
**## Fig D2
clear
svmat c 
rename c1 year
rename (c2 c3 c4 c5 c6 c7 c8 c9) 		(b_ac se_ac  ub01_ac lb01_ac ub05_ac lb05_ac ub10_ac lb10_ac)
rename (c10 c11 c12 c13 c14 c15 c16 c17) (b_wi se_wi ub01_wi lb01_wi ub05_wi lb05_wi ub10_wi lb10_wi)
replace year = 1900+year

// because these were coded as ln(non-prod/prod) we need to multiply by -1 to match the rest of the paper 
// which relied on ln(prod/non-prod)

foreach v in b_ac se_ac  ub01_ac lb01_ac ub05_ac lb05_ac ub10_ac lb10_ac b_wi se_wi ub01_wi lb01_wi ub05_wi lb05_wi ub10_wi lb10_wi{
replace `v' = -1*`v'
}

* Within 
tw rarea ub01_wi lb01_wi year , fc(gs2%05) lp(solid) lc(green) lw(thin) ///
|| rarea ub05_wi lb05_wi year , fc(gs2%05) lp(dash)  lc(purple) lw(medthin) ///
|| rarea ub10_wi lb10_wi year , fc(gs2%05) lp(dash_dot) lc(red) lw(medium)  /// 
|| scatter b_wi year , m(d) msize(large) mfcolor(white)  mc(gs2) ///
yline(0, lp(solid))    ///
legend(order(1 "99% CI" 2 "95% CI" 3 "90% CI" 4 "Point Esimate") pos(6) rows(1) size(large)) ///
xlabel(1980(1)1988, labsize(large)) ///
xtitle("") ///
ylabel(, labsize(large)) ///
ytitle("Coefficient on {&Delta}ln(1+AVE)", size(large))
graph export "$figures/fig_D2_panel_a.png", replace width(1200) height(600)

* Across
tw rarea ub01_ac lb01_ac year , fc(gs2%05) lp(solid) lc(green) lw(thin) ///
|| rarea ub05_ac lb05_ac year , fc(gs2%05) lp(dash)  lc(purple) lw(medthin) ///
|| rarea ub10_ac lb10_ac year , fc(gs2%05) lp(dash_dot) lc(red) lw(medium)  /// 
|| scatter b_ac year , m(d) msize(large) mfcolor(white)  mc(gs2) ///
yline(0, lp(solid))    ///
legend(order(1 "99% CI" 2 "95% CI" 3 "90% CI" 4 "Point Esimate") pos(6) rows(1) size(large)) ///
xlabel(1980(1)1988, labsize(large)) ///
xtitle("") ///
ylabel(, labsize(large)) ///
ytitle("Coefficient on {&Delta}ln(1+AVE)", size(large))
graph export "$figures/fig_D2_panel_b.png", replace width(1200) height(600)
 }

log close
